from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.utils.translation import ugettext_lazy as _
from disn.utilities import *
import logging, datetime, time, qrcode, os
from django.db import IntegrityError
from uuid import uuid4
from disn.settings import PROJECT_PATH, BASE_DIR
from django.db.models import Count
from xpinyin import Pinyin
pinyin = Pinyin()

CATEGORY_ITEMS = (
    ('0', '词典导入'),
    ('1', '自定义'),
)

STARS = (
    ('0', '☆☆☆☆☆'),
    ('1', '★☆☆☆☆'),
    ('2', '★★☆☆☆'),
    ('3', '★★★☆☆'),
    ('4', '★★★★☆'),
    ('5', '★★★★★'),
)

class Domain(models.Model):
    word = models.CharField(max_length=50, blank=True, null=True, verbose_name='词语')
    word_pinyin = models.CharField(max_length=100, blank=True, null=True, verbose_name='拼音')
    domain = models.CharField(max_length=100, blank=True, null=True, verbose_name='域名')
    domain_available = models.BooleanField(default=False, verbose_name='可用')
    domain_expire_date = models.DateField(verbose_name='到期', blank=True, null=True)
    word_addreviation = models.CharField(max_length=50, blank=True, null=True, verbose_name='拼音缩写')
    word_addreviation_available = models.BooleanField(default=False, verbose_name='可用addrev')
    word_addreviation_expire_date = models.DateField(verbose_name='到期addrev', blank=True, null=True)
    category = models.CharField(max_length=2, blank=True, null=True, verbose_name='类别', choices=CATEGORY_ITEMS)
    register_provider = models.CharField(max_length=100, blank=True, null=True, verbose_name='注册商')
    contact   = models.CharField(max_length=100, blank=True, null=True, verbose_name='联系人')
    contact_email = models.CharField(max_length=100, blank=True, null=True, verbose_name='联系邮箱')
    stars = models.CharField(max_length=2, blank=True, null=True, verbose_name='重要程度', default='0', choices=STARS)
    processed = models.BooleanField(default=False, verbose_name='已处理')
    create_at = models.DateTimeField(auto_now_add=True,verbose_name='创建时间')
    modify_at = models.DateTimeField(blank=True,null=True,verbose_name='修改时间')

    class Meta:
        verbose_name = '域名'
        verbose_name_plural = '域名'

    def save(self, *args, **kwargs):
        if self.word:
            self.word_addreviation   = pinyin.get_initials(self.word, '')
            self.word_pinyin = pinyin.get_pinyin(self.word,'')
        self.modify_at = datetime.datetime.today()
        return super(Domain, self).save(*args, **kwargs)

    def __str__(self):
        return self.word


class Series(models.Model):
    name = models.CharField(max_length=50, null=True, blank=True, verbose_name='丛书系列')
    name_pinyin = models.CharField(max_length=200, null=True, blank=True, verbose_name='书名全拼')
    name_py_addrev = models.CharField(max_length=100, null=True, blank=True, verbose_name='书名缩写')
    create_at = models.DateTimeField(auto_now_add=True,verbose_name='创建时间')
    modify_at = models.DateTimeField(blank=True,null=True,verbose_name='修改时间')

    class Meta:
        verbose_name = '系列丛书'
        verbose_name_plural = '系列丛书'

    def save(self, *args, **kwargs):
        if self.name:
            self.name_py_addrev   = pinyin.get_initials(self.name, '')
            self.name_pinyin = pinyin.get_pinyin(self.name,'')
        self.modify_at = datetime.datetime.today()
        return super(Series, self).save(*args, **kwargs)

    def __str__(self):
        return self.name


class Book(models.Model):
    name = models.CharField(max_length=100, null=True, blank=True, verbose_name='书名')
    name_pinyin = models.CharField(max_length=200, null=True, blank=True, verbose_name='书名全拼')
    name_py_addrev = models.CharField(max_length=100, null=True, blank=True, verbose_name='书名缩写')
    author = models.CharField(max_length=100, null=True, blank=True, verbose_name='作者')
    translator = models.CharField(max_length=100, null=True, blank=True, verbose_name='译者')
    publisher  = models.CharField(max_length=50,  null=True, blank=True, verbose_name='出版社')
    publish_date = models.CharField(max_length=10, null=True, blank=True, verbose_name='出版年月')
    num_of_page = models.IntegerField(default=0, verbose_name='页数')
    price = models.DecimalField(max_digits=6, decimal_places=2,null=True, blank=True, verbose_name='定价')
    series = models.ForeignKey(Series, verbose_name='系列丛书')
    isbn = models.CharField(max_length=15, null=True, blank=True, verbose_name='ISBN编号')
    douban_rating = models.DecimalField(max_digits=2, decimal_places=1, null=True, blank=True, verbose_name='豆瓣评分')
    persons_of_rating = models.IntegerField(default=0, verbose_name='投票人数')
    buy_link = models.CharField(max_length=100, null=True, blank=True, verbose_name='购买链接')
    create_at = models.DateTimeField(auto_now_add=True,verbose_name='创建时间')
    modify_at = models.DateTimeField(blank=True,null=True,verbose_name='修改时间')

    class Meta:
        verbose_name = '图书'
        verbose_name_plural = '图书'

    def save(self, *args, **kwargs):
        if self.name:
            self.name_py_addrev   = pinyin.get_initials(self.name, '')
            self.name_pinyin = pinyin.get_pinyin(self.name,'')
        self.modify_at = datetime.datetime.today()
        return super(Book, self).save(*args, **kwargs)

    def __str__(self):
        return self.name
